package com.xiaohe66.web.application.task.convert;

import com.xiaohe66.web.application.common.convert.TagBoConverter;
import com.xiaohe66.web.application.common.result.TagResult;
import com.xiaohe66.web.application.task.bo.TaskSaveBo;
import com.xiaohe66.web.application.task.result.TaskDetailResult;
import com.xiaohe66.web.application.task.result.TaskResult;
import com.xiaohe66.web.domain.account.value.AccountId;
import com.xiaohe66.web.domain.common.agg.Tag;
import com.xiaohe66.web.domain.common.repository.TagRepository;
import com.xiaohe66.web.domain.common.value.TagId;
import com.xiaohe66.web.domain.task.agg.Task;
import com.xiaohe66.web.domain.task.value.TaskChangeTime;
import com.xiaohe66.web.infrastructure.domain.adapter.task.TaskConverter;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * @author xiaohe
 * @since 2021.11.17 14:45
 */
@Mapper
public abstract class TaskBoConverter implements TaskConverter {

    @Autowired
    private TagRepository tagRepository;

    @Autowired
    private TagBoConverter tagBoConverter;

    /*public TaskPoolResult convert(List<Task> originList) {

        Map<LocalDate, List<TaskDateResult.Item>> map = new LinkedHashMap<>();

        for (Task task : originList) {

            LocalDate localDate = task.getChangeTime().getValue().toLocalDate();

            List<TaskDateResult.Item> list = map.computeIfAbsent(localDate, k -> new ArrayList<>());
            list.add(toResult(task));
        }

        List<TaskDateResult> resultList = new ArrayList<>(map.size());

        for (Map.Entry<LocalDate, List<TaskDateResult.Item>> entry : map.entrySet()) {

            final TaskDateResult result = new TaskDateResult();
            result.setDate(entry.getKey());
            result.setList(entry.getValue());

            resultList.add(result);
        }

        TaskPoolResult poolResult = new TaskPoolResult();

        poolResult.setQty(originList.size());
        poolResult.setDateList(resultList);

        return poolResult;
    }*/

    public abstract Task toAgg(TaskSaveBo task, AccountId createId);

    @Mapping(target = "changeTime", dateFormat = "yyyy-MM-dd")
    public abstract TaskResult toResult(Task task);

    public abstract List<TaskResult> toResult(List<Task> task);

    @Mapping(target = "changeTime", dateFormat = "yyyy-MM-dd")
    public abstract TaskDetailResult toDetail(Task task);

    protected TagResult toTagResult(TagId tagId) {

        Tag tag = tagRepository.getById(tagId);
        return tagBoConverter.toResult(tag);
    }

    protected String toTagName(TagId tagId) {

        Tag tag = tagRepository.getById(tagId);
        return tag.getName().getValue();
    }

    protected TagId toTagIds(Long tagId) {
        return ifPresent(tagId, TagId::new);
    }

    protected TaskChangeTime newTaskChangeTime(LocalDate changeTime){
        if(changeTime == null){
            return null;
        }
        LocalDateTime localDateTime = LocalDateTime.of(changeTime, LocalTime.now());
        return new TaskChangeTime(localDateTime);
    }
}
